package com.itmuch.cloud.moviecustomer.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.itmuch.cloud.moviecustomer.entity.User;
import com.itmuch.cloud.moviecustomer.feign.UserFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@RestController
public class UserController {
	@Autowired
	@LoadBalanced
	private RestTemplate restTemplate;
	
	@Autowired
	private UserFeign userFeign;
	
	@GetMapping("/findUserByid/{id}")
	@HystrixCommand(fallbackMethod = "defaultUser")//请求超时就调用defaultUser方法
	public User findById(@PathVariable Long id) {
		return restTemplate.getForObject("http://movie-service/findUserByid/"+id, User.class);
	}
	
	public User defaultUser(Long id) {
		User user = new User();
		user.setId(id);
		user.setName("江安明");
		user.setAge(30);
		user.setUsername("jianganming");
		user.setBalance(new BigDecimal("300"));
		return user;
	}
	
	
	
	@GetMapping("/findAll")
	public List<User> findAll() {
		User[] users = restTemplate.getForObject("http://movie-service/findAll", User[].class);
		List<User> userList = Arrays.asList(users);
		return userList;
	}
	
	@GetMapping("/getUserByid/{id}")
	@HystrixCommand(fallbackMethod = "defaultUser")
	public User getById(@PathVariable Long id) {
		return userFeign.findUserByid(id);
	}
	
	
}
